﻿#define T_Common
using UnityEngine;
using System.Collections;
using System;


public class NScrollViewInfinitePage : MonoBehaviour
{
    private float m_PageWidth;
    public int m_EffortsFlip = 50;
    public bool m_IsNotRelativeMove;
    private int m_PageNums = 0;
    [SerializeField]
    private RollCookie m_RollCookie;
    private int m_PageIndex = 0;                             // 当前所在页 

    private UIScrollView m_ScrollView = null;
    private float m_NowLocation;
    private bool m_IsSpringMove;
    private bool m_IsDrag;
    private SpringPanel m_Sp;
    private bool m_IsHorizontal = true;

#if T_Common
    #region 调试数据
    private float nowDistance = 0;
    #endregion

#endif

    #region 属性
    public int PageIndex
    {
        get
        {
            return m_PageIndex;
        }
    }

    public int PageIntervalIndex
    {
        get
        {
            return ConvertCircleIndex(m_PageIndex, m_RollCookie.Page);
        }
    }

    #endregion

    private void Awake()
    {
        m_ScrollView = gameObject.GetComponent<UIScrollView>();
        m_Sp = gameObject.AddComponent<SpringPanel>();

        m_ScrollView.onDragStarted = OnDragStarted;
        m_ScrollView.onMomentumMove = OnMomentumMove;
        m_ScrollView.onStoppedMoving = OnStoppedMoving;

        if (m_ScrollView.movement == UIScrollView.Movement.Horizontal)
            m_IsHorizontal = true;
        else
            m_IsHorizontal = false;

        m_PageWidth = GetComponent<UIPanel>().GetViewSize().x;


        OnStoppedMoving();
    }

    private void OnDragStarted()
    {
        m_IsDrag = false;
        SetNowLocation();
    }

    private void OnMomentumMove()
    {
        if (m_IsDrag) return;

        Vector3 v3 = transform.localPosition;
        float value = 0;
        if (m_IsHorizontal)
        {
            value = m_NowLocation - v3.x;
            nowDistance = value;
        }
        else
        {
            value = m_NowLocation - v3.y;
        }

        if (Mathf.Abs(value) < m_EffortsFlip)
            return;

        if (value > 0)
        {
            m_PageNums--;
            m_PageIndex++;
            Page(m_PageWidth);
        }
        else
        {
            m_PageNums++;
            m_PageIndex--;
            Page(m_PageWidth);
        }
    }

    private void SpringPanleMoveEnd()
    {
        m_IsSpringMove = false;
        //Debug.Log("m_PageIndex: " + m_PageIndex + "  m_PageNums: " + m_PageNums);
        m_RollCookie.MoveInterval(m_PageNums);
    }

    private void OnStoppedMoving()
    {
        m_IsDrag = false;
        SetNowLocation();
    }

    private void Page(float value)
    {
        m_IsSpringMove = true;
        m_IsDrag = true;

        m_Sp.enabled = false;
        Vector3 pos = m_Sp.target;
        if (!m_IsNotRelativeMove)
            pos = m_IsHorizontal ? new Vector3(pos.x + value, pos.y, pos.z) : new Vector3(pos.x, pos.y + value, pos.z);
        else
            pos = m_IsHorizontal ? new Vector3(value * m_PageNums, pos.y, pos.z) : new Vector3(pos.x, m_PageNums * value, pos.z);


        //Debug.Log("移动距离: " + value + " - " + Mathf.Abs(m_PageNums));

        SpringPanel.Begin(gameObject, pos, 13f).strength = 8f;
        m_Sp.onFinished = SpringPanleMoveEnd;

        //Debug.Log("page index=" + mPageIndex);
    }

    private void SetNowLocation()
    {
        if (m_IsHorizontal)
        {
            m_NowLocation = gameObject.transform.localPosition.x;
        }
        else
        {
            m_NowLocation = gameObject.transform.localPosition.y;
        }
    }

    private int ConvertCircleIndex(int index, int circlelen)
    {
        if (index < 0)
        {
            index = Math.Abs(index);

            if (index > circlelen)
                index = index % circlelen;

            index = (circlelen - index) % circlelen;
        }
        else
        {
            index = Math.Abs(index);
            index = index % circlelen;
        }
        return index;
    }

#if T_Common
    public void OnGUI()
    {
        GUILayout.Label("原始位置: " + m_NowLocation);
        GUILayout.Label("现在位置: " + transform.localPosition);
        GUILayout.Label("移动的距离(m_EffortsFlip): " + m_EffortsFlip);
        GUILayout.Label("啦的距离(nowDistance): " + nowDistance);
        GUILayout.Label("总距离: " + (m_PageNums - 1) * -m_PageWidth);
        GUILayout.Label("页码: " + m_PageNums);
    }
#endif

    #region 方法
    public void PreviousPage()
    {
        if (m_IsHorizontal)
        {
            if (m_PageIndex > 0) Page(m_PageWidth);
        }
        else
        {
            if (m_PageIndex < m_PageNums) Page(m_PageWidth);
        }
    }

    public void NextPage()
    {
        if (m_IsHorizontal)
        {
            if (m_PageIndex < m_PageNums) Page(-m_PageWidth);
        }
        else
        {
            if (m_PageIndex > 0) Page(-m_PageWidth);
        }
    }

    public void GoToPage(int page)
    {
        if (m_IsHorizontal)
        {
            if (m_PageIndex < page)
            {
                Page(-m_PageWidth * (page - m_PageIndex));
            }
            else if (m_PageIndex > page)
            {
                Page(m_PageWidth * (m_PageIndex - page));
            }
        }
        else
        {
            if (m_PageIndex > 0) Page(-m_PageWidth);
        }
    }


    #endregion
}